﻿using System;
using System.Windows.Forms;
using MyFinanceManager.Controls;
using MyFinanceManager.Objects;

namespace MyFinanceManager
{
    public partial class NewExpense : Form
    {
        private int step = -1;
        //private int amountOfSteps = 7;
        private int amountOfSteps = 6;
        private int subCatStep = 5;

        ucAmount betrag;
        ucDate datum;
        ucTime uhrzeit;
        ucDescription beschreibung;
        ucCategory kategorie;
        ucCategorySub subkategorie;
        int forSubKat = -1;
        //ucAccount konto;
        ucSave save;
        EventHandler<StringEventArg> stringEventHandler = delegate { };

        PostingRecord datensatz = new PostingRecord();

        public NewExpense()
        {
            this.stringEventHandler = new EventHandler<StringEventArg>(item_NewValueAvailable);
            InitializeComponent();

            miVor_Click(this, null);
        }

        private void miZurück_Click(object sender, EventArgs e)
        {
            this.step--;
            if (this.step == this.subCatStep && !Repository.RepositoryCenterEx.HasSubCat(this.forSubKat))
                this.step--;

            ChangeUserControl();
        }

        private void miVor_Click(object sender, EventArgs e)
        {
            if (this.miVor.Text == "Exit")
            {
                this.Dispose();
            }
            else
            {
                this.step++;
                if (this.step == this.subCatStep && !Repository.RepositoryCenterEx.HasSubCat(this.forSubKat))
                    this.step++;
                ChangeUserControl();
            }
        }

        private void ChangeUserControl()
        {
            SaveDateIfPossible();
            CreateIfNecessary();

            foreach (Control var in this.panel1.Controls)
            {
                var.Hide();
            }

            ShowRightUserControl();
        }

        private void ShowRightUserControl()
        {
            this.Refresh();

            this.miZurück.Enabled = !(step < 1);
            if (step > this.amountOfSteps)
            {
                this.miVor.Text = " ";

                this.laTitle.Text = "Speichere Eintrag";
                this.Refresh();

                try
                {
                    this.save.Value = "Versuche Datenbank zu initialisieren";
                    Database ds = Database.Db;
                    this.save.Value += "\r\nVersuch geglückt";

                    this.save.Value += "\r\nFüge Eintrag hinzu";
                    ds.Expenses.Add(this.datensatz);
                    this.save.Value += "\r\nSpeichere Datenbank";
                    ds.Save();
                    this.save.Value += "\r\nAbgeschlossen";
                }
                catch (Exception ex)
                {
                    this.save.Value += "\r\nFehler:";
                    this.save.Value += "\r\n" + ex.Message;

                }

                this.laTitle.Text = "Eintrag gespeichert";
                this.miVor.Text = "Exit";
            }
            else
            {
                this.miVor.Text = "Vor";
                if (step == this.amountOfSteps)
                    this.miVor.Text = "Save";
            }

            switch (step)
            {
                case 0:
                    this.laTitle.Text = "Betrag eingeben";
                    this.betrag.Show();
                    break;
                case 1:
                    this.laTitle.Text = "Datum eingeben";
                    this.datum.Show();
                    break;
                case 2:
                    this.laTitle.Text = "Uhrzeit eingeben";
                    this.uhrzeit.Show();
                    break;
                case 3:
                    this.laTitle.Text = "Beschreibung eingeben";
                    this.beschreibung.Show();
                    break;
                case 4:
                    this.laTitle.Text = "Kategorie angeben";
                    this.kategorie.Show();
                    break;
                case 5:
                    this.laTitle.Text = "Subkategorie angeben";
                    this.subkategorie.Show(this.forSubKat);
                    break;
                //case 6:
                //    this.laTitle.Text = "Konto angeben";
                //    this.konto.Show();
                //    break;
                case 6:
                    this.laTitle.Text = "Speichere";
                    if (step == this.amountOfSteps)
                        this.save.Value = datensatz.ToString();
                    this.save.Show();
                    break;
            }
        }

        private void CreateIfNecessary()
        {
            switch (step)
            {
                case 0:
                    if (this.betrag == null)
                        this.betrag = new ucAmount();
                    this.panel1.Controls.Add(this.betrag);
                    break;
                case 1:
                    if (this.datum == null)
                        this.datum = new ucDate();
                    this.panel1.Controls.Add(this.datum);
                    break;
                case 2:
                    if (this.uhrzeit == null)
                        this.uhrzeit = new ucTime();
                    this.panel1.Controls.Add(this.uhrzeit);
                    break;
                case 3:
                    if (this.beschreibung == null)
                        this.beschreibung = new ucDescription();
                    this.panel1.Controls.Add(this.beschreibung);
                    break;
                case 4:
                    if (this.kategorie == null)
                        this.kategorie = new ucCategory(Repository.RepositoryCenterEx);
                    this.panel1.Controls.Add(this.kategorie);
                    break;
                case 5:
                    if (this.subkategorie == null)
                        this.subkategorie = new ucCategorySub(Repository.RepositoryCenterEx);
                    this.panel1.Controls.Add(this.subkategorie);
                    break;
                //case 6:
                //    if (this.konto == null)
                //        this.konto = new ucAccount();
                //    this.panel1.Controls.Add(this.konto);
                //    break;
                case 6:
                    if (this.save == null)
                        this.save = new ucSave();
                    this.panel1.Controls.Add(this.save);
                    break;
                default:
                    break;
            }

            foreach (FiringUserControl item in this.panel1.Controls)
            {
                item.NewValueAvailable -= this.stringEventHandler;
                item.NewValueAvailable += this.stringEventHandler;
            }
        }

        void item_NewValueAvailable(object sender, StringEventArg e)
        {
            this.laTitle.Text = "Gewählt: " + e.Info;

            if (sender == this.kategorie)
                this.forSubKat = kategorie.ValueAsInt;
        }

        /// <summary>
        /// Saves the date if possible.
        /// </summary>
        private void SaveDateIfPossible()
        {
            if (this.betrag != null)
                this.datensatz.Amount = this.betrag.Value;
            if (this.datum != null)
                this.datensatz.Time = this.datum.Value;

            if (this.uhrzeit != null)
            {
                this.datensatz.Time = new DateTime(this.datensatz.Time.Year,
                    this.datensatz.Time.Month,
                    this.datensatz.Time.Day,
                    this.uhrzeit.Value.Hour,
                    this.uhrzeit.Value.Minute,
                    0);
            }
            if (this.beschreibung != null)
                this.datensatz.Description = this.beschreibung.Value;

            //if (this.konto != null)
            //this.datensatz.AccountName = this.konto.Value;

            if (this.kategorie != null)
                this.datensatz.CategoryName = this.kategorie.Value;

            if (this.subkategorie != null && Repository.RepositoryCenterEx.HasSubCat(this.forSubKat))
                this.datensatz.SubCategoryName = this.subkategorie.Value;
            else
                this.datensatz.SubCategoryName = string.Empty;
        }
    }
}